using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using RPGOnline.Properties;
using RPGOnline.Rendering;

namespace RPGOnline.Controls.Map.Layers
{
    public class BackgroundLayer : ILayer
    {
		private readonly Renderer m_renderer;
        private readonly Map m_map;
        private Texture m_backGroundTexture;
	    private Matrix m_renderMatrix;
	    private Matrix m_textureMatrix;

	    public BackgroundLayer(Renderer renderer, Map map)
        {
            m_renderer = renderer;
            m_map = map;

			int width = m_map.MapSize.Width*m_map.TileSize.Width;
            int height = m_map.MapSize.Height*m_map.TileSize.Height;

            /*m_backGround = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured), 4, m_renderer, Usage.WriteOnly, CustomVertex.TransformedColoredTextured.Format, Pool.Managed);
            var vertices = new CustomVertex.TransformedColoredTextured[4];

            int width = m_map.MapSize.Width*m_map.TileSize.Width;
            int height = m_map.MapSize.Height*m_map.TileSize.Height;

            vertices[0].Position = new Vector4(0, 0, 0, 1.0f);
            vertices[0].Color = Color.White.ToArgb();
            vertices[0].Tu = 0;
            vertices[0].Tv = 0;

            vertices[1].Position = new Vector4(width, 0, 0, 1.0f);
            vertices[1].Color = Color.White.ToArgb();
            vertices[1].Tu = m_map.MapSize.Width * 2;
            vertices[1].Tv = 0;

            vertices[2].Position = new Vector4(0, height, 0, 1.0f);
            vertices[2].Color = Color.White.ToArgb();
            vertices[2].Tu = 0;
            vertices[2].Tv = m_map.MapSize.Height * 2;

            vertices[3].Position = new Vector4(width, height, 0, 1.0f);
            vertices[3].Color = Color.White.ToArgb();
            vertices[3].Tu = m_map.MapSize.Width * 2;
            vertices[3].Tv = m_map.MapSize.Height * 2;

            m_backGround.SetData(vertices, 0, LockFlags.None);*/
            m_backGroundTexture = new Texture(m_renderer.Device, Resources.Background, Usage.None, Pool.Managed);
	        m_renderMatrix = Matrix.Scaling(width, height, 1);
		    m_textureMatrix = Matrix.Scaling(m_map.MapSize.Width*2, m_map.MapSize.Height*2, 1);
        }

        public void Clear()
        {
        }

        public void Draw()
        {
			//m_textureMatrix = Matrix.Scaling(1.0f / (m_map.MapSize.Width * 2), 1.0f / (m_map.MapSize.Height * 2), 1);
			m_renderer.DrawQuad(m_renderMatrix, m_textureMatrix, m_backGroundTexture);
        }
    }
}